//
//  ClassCouresTimeViewController.m
//  HTP
//
//  Created by admin on 15/10/30.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "ClassCouresTimeViewController.h"
#import "FSCalendar.h"
#import "CourseCell.h"
#import "NSDate+FSExtension.h"
#import "AppDelegate.h"
#import "ClassCourseDetailViewController.h"
#define  CALENDARHEIGHT 200
#define CELLHEIGHT_Course 45
@interface ClassCouresTimeViewController ()<FSCalendarDataSource,FSCalendarDelegate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) FSCalendar *courseCalendar;
@property (nonatomic,retain) NSDate     *selectDate;

@property (nonatomic,strong) BaseTableView *courseTable;
@end

@implementation ClassCouresTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.courseCalendar];
    [self addWeekendBg];
    [self _initFootView];
    [self.courseCalendar selectDate:[[NSDate date] fs_dateBySubtractingDays:2] scrollToDate:YES];
    [self.view addSubview:self.courseTable];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self addCustomBackWithAction:@selector(backToQM) normalImg:SYSTEMBACKIMAGE selectImg:SYSTEMBACKIMAGE withPadding:5];
    [self addCustomTitleWithTitle:@"我的课程"];
    [self _initRightNav];
    
}
//TODO:绘制UI
/**
 *  底部UI绘制
 yunhe
 */
-(void)_initFootView{
    UIImageView *footImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bottombg"]];
    [footImg setFrame:CGRectMake(0, ScreenHeihgt - 49 - NavHe, ScreenWidth, 49)];
    [self.view addSubview:footImg];
    [self.view bringSubviewToFront:footImg];
    UIButton *today = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth / 2.0, 49)];
    [today setTitle:@"今天" forState:UIControlStateNormal];
    [today setTitleColor:RGBCOLOR(250, 153, 54, 0.9) forState:UIControlStateNormal];
    today.titleLabel.font = UIFONTSYS(20);
    today.titleLabel.textAlignment = _TEXT_ALIGN_LEFT;
    today.titleEdgeInsets = UIEdgeInsetsMake(0, -ScreenWidth / 4.0, 5, 0);
    [footImg addSubview:today];
    
    UIButton *all = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth / 2.0, 0, ScreenWidth / 2.0, 49)];
    [all setTitle:@"课程一览" forState:UIControlStateNormal];
    [all setTitleColor:RGBCOLOR(250, 153, 54, 0.9) forState:UIControlStateNormal];
    all.titleLabel.font = UIFONTSYS(20);
    all.titleLabel.textAlignment = _TEXT_ALIGN_RIGHT;
    all.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 5, -ScreenWidth / 4.0+40);
    [footImg addSubview:all];
}
/**
 *  yunhe --- 导航栏右侧按钮
 */
-(void)_initRightNav{
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = 20;
    UIButton *searchBtn = [self creatButtonWithImage:[UIImage imageNamed:@"search"] imageSel:[UIImage imageNamed:@"search"] title:@"" observer:self action:@selector(searchAction) beginPosition:CGPointMake(0, 0) isBack:NO];
    
    UIButton *addBtn = [self creatButtonWithImage:[UIImage imageNamed:@"add"] imageSel:[UIImage imageNamed:@"add"] title:@"" observer:self action:@selector(addAction) beginPosition:CGPointMake(0, 0) isBack:NO];
    
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:addBtn],negativeSpacer,[[UIBarButtonItem alloc] initWithCustomView:searchBtn]];
}
//TODO:重写get方法
/**
 *  初始化日历
    yunhe
 *
 *  @return FSCalendar
 */
-(FSCalendar *)courseCalendar{
    if (!_courseCalendar) {
        _courseCalendar = [[FSCalendar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, CALENDARHEIGHT)];
        _courseCalendar.dataSource = self;
        _courseCalendar.delegate = self;
        _courseCalendar.today = [NSDate date];
        _courseCalendar.scrollDirection = FSCalendarScrollDirectionVertical;
        _courseCalendar.currentPage = [NSDate date];
        _courseCalendar.allowsMultipleSelection = NO;
        _courseCalendar.backgroundColor = [UIColor clearColor];
        _courseCalendar.headerHeight = 0;
        _courseCalendar.weekdayHeight = 20;
        _courseCalendar.appearance.weekdayTextColor = [UIColor whiteColor];
        _courseCalendar.appearance.headerTitleColor = [UIColor redColor];
        _courseCalendar.appearance.eventColor = [UIColor clearColor];
        _courseCalendar.appearance.cellShape =  FSCalendarCellShapeCircle;
        _courseCalendar.appearance.caseOptions = FSCalendarCaseOptionsWeekdayUsesDefaultCase;
        _courseCalendar.appearance.selectionColor = [UIColor greenColor];
        _courseCalendar.appearance.todayColor = [UIColor orangeColor];
//        _courseCalendar.appearance.todaySelectionColor = [UIColor blackColor];
    }
    return _courseCalendar;
}
-(void)addWeekendBg{
    UIImageView *backImage = [[UIImageView alloc] init];
    backImage.image = [UIImage imageNamed:@"navBG"];
    [backImage setFrame:CGRectMake(0, 0, ScreenWidth, 20)];
    [self.view addSubview:backImage];
    [self.view sendSubviewToBack:backImage];
    
    UIImageView *back_1 = [[UIImageView alloc] init];
    back_1.image = [UIImage imageNamed:@"courseBackground"];
    [back_1 setFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt)];
    [self.view addSubview:back_1];
    [self.view sendSubviewToBack:back_1];
}
/**
 *  课程tableView
 *
    yunhe
 *  @return
 */
-(BaseTableView *)courseTable{
    if (!_courseTable) {
        _courseTable                  = [[BaseTableView alloc] initWithFrame:CGRectMake(0, CALENDARHEIGHT, ScreenWidth, ScreenHeihgt - NavHe-CALENDARHEIGHT - 49) style:UITableViewStylePlain withHeadRefresh:NO andFoodRefresh:NO];
        _courseTable.dataSource       = self;
        _courseTable.delegate         = self;
        _courseTable.separatorStyle   = UITableViewCellSeparatorStyleNone;
        _courseTable.backgroundColor  = [UIColor whiteColor];
    }
    return _courseTable;
}
//TODO:FSCalendar delegate
/**
 *  按钮选择事件
 *
 *  @param calendar
 *  @param date
 */
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date{
    if (_selectDate != nil && [_selectDate fs_isEqualToDateForDay:date]) {
        return;
    }else{
        _selectDate = date;
    }
}
//TODO:FSCalendar datasource
- (NSString *)calendar:(FSCalendar *)calendar subtitleForDate:(NSDate *)date{
    if ([date fs_isEqualToDateForDay:[NSDate date]]) {
        return @"今天";
    }
   return @"";
}
- (BOOL)calendar:(FSCalendar *)calendar hasEventForDate:(NSDate *)date{
    return YES;
}
/**
 *  日期下方image
 *
 *  @param calendar
 *  @param date
 *
 *  @return
 */
-(UIImage *)calendar:(FSCalendar *)calendar imageForDate:(NSDate *)date{
    if ([date fs_isEqualToDateForMonth:[NSDate date]]) {
        NSInteger days = [date fs_daysFrom:[NSDate date]];
        if (days % 4 == 1) {
            return [UIImage imageNamed:@"day_select_y"];
        }else if (days % 4 == 2){
            return [UIImage imageNamed:@"day_select_g"];
        }else if (days % 4 == 3){
            return [UIImage imageNamed:@"day_select_h"];
        }else{
            return nil;
        }
    }
    return nil;
}
//- (NSDate *)minimumDateForCalendar:(FSCalendar *)calendar{
//    
//}
//- (NSDate *)maximumDateForCalendar:(FSCalendar *)calendar{
//    
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//TODO:tabledelegate datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = UIView.new;
    [view setFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    [view setBackgroundColor:[UIColor clearColor]];
    return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifity = @"courseCell";
    CourseCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifity];
    if (!cell) {
        cell = [[CourseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifity];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setCellData:[NSString stringWithFormat:@"%ld",indexPath.section]];
    [cell setFirstImg:indexPath.row];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ClassCourseDetailViewController *ccDetailVc = [[ClassCourseDetailViewController alloc] init];
    ccDetailVc.courseState = indexPath.row % 3;
    [self.navigationController pushViewController:ccDetailVc animated:YES];
}

//TODO:action
/**
    yunhe
 *  搜索方法
 */
-(void)searchAction{
    
}
/**
    yunhe
 *  添加方法
 */
-(void)addAction{
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
